
dojo.debugReturn = function(obj){
    if (!djConfig.isDebug) { return; }
    var debug = "\n";
    debug += 'Object: ' + obj + '\n';
    var props = [];
    for(var prop in obj){
        try {
            props.push(prop + ': ' + obj[prop]);
        } catch(E) {
            props.push(prop + ': ERROR - ' + E.message);
        }
    }
    props.sort();
    for(var i = 0; i < props.length; i++) {
        debug += props[i]+'\n';
    }
    debug += '\n';
    return debug;
}

dojo.require("dojo.parser");
dojo.require("dojo.rpc.JsonService");
dojo.require("dijit.dijit");
dojo.require("dijit.form.Button");
dojo.require("dijit.Toolbar");
dojo.require("dijit.Menu");
dojo.require("dijit.layout.BorderContainer");
dojo.require("dijit.layout.ContentPane");
dojo.require("dojox.collections.Dictionary");

var ctrl = null;

dojo.addOnLoad( function () {
	//dojo.parser.parse();
	dojo.style(dojo.byId("simp_container"), "height", (window.innerHeight - 90) + "px");
	console.debug("create Controller");
	ctrl = new Controller(dijit.byId("simp_imageEditArea"), dijit.byId("simp_messageBox"), "{{base_url}}", dojo.byId("simp_image").value, dojo.byId("simp_instance").value)
});

dojo.provide("Controller");
dojo.declare("Controller", null, {
	rpc: null,
	instance: 0,
	image: "/simp/logo.png",
	baseUrl: "/simp/",
	messageBox: null,
	editorArea: null,
	module: "view",
	defaultModule: "view",
	preset: -1,
	values: {},
	editors: {},
	
	constructor: function(editorArea, messageBox, baseUrl, file, instance) {
		this.rpc = new dojo.rpc.JsonService("/simp/rpc/?SMD=True");
		this.baseUrl = baseUrl
		this.editorArea = editorArea	
		this.messageBox = messageBox
		this.file = file
		this.instance = instance
		this.image = dojo.fromJson(dojo.byId("simp_image").value)
		this.editors = new dojox.collections.Dictionary()
	},
	
	registerEditor: function(editor) {
		if (!this.editors.containsKey(editor.getModuleName())) {
			console.debug("register editor of the module: "+editor.getModuleName())
			this.editors.add(editor.getModuleName(), editor)
		}
	},
	
	setCurrentEditor: function(module) {
		this.editors.item(this.module).deactivate()
		this.module = module
		if (this.editors.containsKey(module)) {
			var editor = this.editors.item(module);
			this.editorArea.setContent(editor.getEditorMarkup());
			//editor.setImage(this.image);
			editor.activate(this.image);
		} else {
			this.getEditor(module);
		}
	},
	
	setPreset: function(preset) {
		this.getPreset(preset);
	},
	
	apply: function(values) {
		// set /simp/module/apply
		// apply returns 
		// imageFile:
		// rsettings:
		//dojo.xhrPost(
		//	this.baseUrl + "apply/" + this.moduleName + "/",
		//alert("apply values: " + dojo.toJson(values));
		this.presetLoaded();
	},
	
	commit: function(values) {
		var deferred = this.rpc.commit(this.module, this.preset, values);
		deferred.addCallback(this.loadCommit);	
	},
	
	loadCommit: function(result, error) {
		console.debug(dojo.debugReturn(result));
		console.debug(dojo.debugReturn(error));
		//ctrl.editorArea.setContent(result.editor);
		ctrl.preset = 0;
		ctrl.module = ctrl.defaultModule
		ctrl.setCurrentEditor(ctrl.defaultModule);
		ctrl.editors.item(ctrl.defaultModule).setValues(result.values);
		ctrl.setImage(result.image);
		//ctrl.setImage(result.image);
		//ctrl.module = result.module
		//ctrl.preset = result.preset
		//ctrl.values = result.values
		//ctrl.editorLoaded();		
	},
	
	getEditor: function(module, callback) {
		//alert ("load editor for module:" + module);
		console.debug("load editor for module: "+module);
		var deferred = this.rpc.editor(module)
		if (callback == null)
			deferred.addCallback(this.loadEditor);
		else
			deferred.addCallback(callback);
	},
	
	loadEditor: function(result, error) {
		if (djConfig.isDebug) {
			console.debug(dojo.debugReturn(result));
			console.debug(dojo.debugReturn(error));
		}
		ctrl.editorArea.setContent(result.editor);
		ctrl.setImage(result.image);
		ctrl.editorLoaded();		
	},
	
	getPreset: function(preset) {
		this.preset = preset;
		var deferred = this.rpc.preset(this.module, preset);
		deferred.addCallback(this.loadPreset);
	},
	
	loadPreset: function(result, error) {
		console.debug("Controller loadPreset: " + dojo.debugReturn(result));
		ctrl.values = result.values;
		if (ctrl.editors.containsKey(ctrl.module)) {
			ctrl.editors.item(ctrl.module).setValues(ctrl.values);
			ctrl.setImage(result.image);
		}
		ctrl.valuesLoaded();
	},
	
	save: function() {
		console.debug("Controller save");
		var deferred = this.rpc.save();
		deferred.addCallback(this.loadCallback);
	},
	
	cancle: function() {
		console.debug("Controller cancle");
		var deferred = this.rpc.cancle();
		deferred.addCallback(this.loadCallback);		
	},
	
	loadCallback: function(result, error) {
		if (result.callback != null) {
			window.location = result.callback;
		}
	},
	
	setImage: function(image) {
		this.image = image;
		dojo.byId("simp_image").value = dojo.toJson(this.image);
		if (this.editors.containsKey(this.module))
			this.editors.item(this.module).activate(this.image);
	},
	
	editorLoaded: function() {
		
	},
	
	valuesLoaded: function() {
	
	}
});

dojo.provide("BaseEditor")
dojo.declare("BaseEditor", null, {
	_moduleName: "BaseEditor",

	getModuleName: function() {
		return this._moduleName;
	},
	
	getEditorMarkup: function() {
		return "<h1>This is the default Editor</h1>";
	},
	
	setPreset: function(preset) {
		
	}, 
	
});

{% for mod in modules %}
{{ mod.get_scripts }}
{% endfor %}